package pro.lingwu.station.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import pro.lingwu.station.dao.DriverMapper;
import pro.lingwu.station.dto.PagesResult;
import pro.lingwu.station.dto.db.Admin;
import pro.lingwu.station.dto.db.Driver;
import pro.lingwu.station.service.DriverService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author @lingwu
 * @date created in 11/19/2021
 */
@Service
public class DriverServiceImpl implements DriverService {

    private final DriverMapper mapper;

    public DriverServiceImpl(DriverMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void addDriver(Driver driver) {
        mapper.addDriver(driver);
    }

    @Override
    public Driver signIn(String account, String password, HttpServletRequest request) {
        Driver driver = mapper.findDriverBy(account);
        if(driver.getPassword().equals(password)){
            HttpSession session = request.getSession();
            session.setAttribute("driver-sign-in", true);
        }
        return driver;
    }

    @Override
    public void updateDriver(Driver driver) {
        mapper.updateDriver(driver);
    }

    @Override
    public PagesResult<Driver> getDrivers(int start, int size) {
        if(size > -1){
            PageHelper.startPage(start, size);
        }
        var res = new PagesResult<Driver>();
        res.setTotal(mapper.getTotal());
        res.setResult(mapper.getDrivers());
        return res;
    }

    @Override
    public void rmDriver(String account) {
        mapper.rmDriver(account);
    }
    
}
